/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __LIB3D_SIZEINITIALIZER_H__
#define __LIB3D_SIZEINITIALIZER_H__

#include "BaseRandomInitializer.h"

namespace liba
{
	namespace lib3d
	{
		namespace particles
		{
			class SizeInitializer : public BaseRandomInitializer<math::Vector2<Float>, offsetof(Particle, currentState.scale)>
			{
			private:
				bool _homogeneous;
			private:
				virtual ~SizeInitializer()
				{
				}
			public:
				SizeInitializer() : _homogeneous(false)
				{
				}
				
				virtual void initialize_particle(Particle * particles)
				{
					if(_homogeneous)
					{
						Float scaleRandom = math::random(_randomMin.x, _randomMax.x);
						*reinterpret_cast<math::Vector2<Float>*>(reinterpret_cast<char*>(particles) + offsetof(Particle, currentState.scale)) = _base + math::Vector2<Float>(scaleRandom, scaleRandom);
					}
					else
					{
						*reinterpret_cast<math::Vector2<Float>*>(reinterpret_cast<char*>(particles) + offsetof(Particle, currentState.scale)) = _base + math::random(_randomMin, _randomMax);
					}
				}
				
				virtual void on_save_attributes(xml::SaverBase * saver) const
				{
					BaseRandomInitializer<math::Vector2<Float>, offsetof(Particle, currentState.scale)>::on_save_attributes(saver);

					if(_homogeneous)
						saver->save_attribute("Homogeneous", _homogeneous);
				}

				virtual bool on_attribute(xml::Provider * provider, const std::string & name, const std::string & value)
				{
					// homogeneous scale
					if(name == "Homogeneous")
						return converter::convert(value, &_homogeneous);
					
					// unknown attribute
					return BaseRandomInitializer<math::Vector2<Float>, offsetof(Particle, currentState.scale)>::on_attribute(provider, name, value);
				}

				void set_homogeneous(bool value)
				{
					_homogeneous = value;
				}

				bool get_homogeneous() const
				{
					return _homogeneous;
				}
			};
		}
	}
}

#endif